package com.baoyouqun.controller.union;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.request.AuthWechatMiniProgramRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SaIgnore
@RestController
@RequestMapping("/wechat")
@RequiredArgsConstructor
public class WechatAuthController {


    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.secret}")
    private String secret;

    @ApiModelProperty("生成授权地址")
    @RequestMapping("/callback")
    public Object callback(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequest();
        AuthResponse<AuthUser> result = authRequest.login(callback);
        return result;
    }

    private AuthRequest getAuthRequest() {
        return new AuthWechatMiniProgramRequest(AuthConfig.builder()
                .clientId(appid)
                .clientSecret(secret)
                .ignoreCheckRedirectUri(true)
                .ignoreCheckState(true)
                .build());
    }


}
